package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.JobOpportunityTechnology;
import com.wolves.career.service.JobOpportunityTechnologyService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 * 技术管理控制列
 */
@RestController
@RequestMapping("/job/technology")
@CrossOrigin
public class JobOpportunityTechnologyController {
    @Autowired
    JobOpportunityTechnologyService technologyService;

    /**
     * 根据id获取技术
     * @param technologyId 技术id
     * @return
     */
    @GetMapping("/getTechnology/{technologyId}")
    public R getTechnology(@PathVariable String technologyId){
        JobOpportunityTechnology technology = technologyService.getById(technologyId);
        return R.ok().data("technology",technology);
    }

    /**
     * 添加技术
     *
     * @param technology 技术类
     * @return
     */
    @PostMapping("/addTechnology")
    public R addTechnology(@RequestBody(required = true) JobOpportunityTechnology technology) {
        boolean save = technologyService.save(technology);
        if (!save) {
            return R.error().message("插入失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 根据技术id删除技术
     *
     * @param technologyId
     * @return
     */
    @DeleteMapping("/removeTechnology/{technologyId}")
    public R removeTechnology(@PathVariable String technologyId) {
        boolean remove = technologyService.removeById(technologyId);
        if (!remove) {
            return R.error().message("删除失败");
        }
        return R.ok().data("message", true);
    }

    /**
     * 更新技术
     *
     * @param technology 技术类
     * @return
     */
    @PutMapping("/updateTechnology")
    public R updateTechnology(@RequestBody(required = true) JobOpportunityTechnology technology) {
        boolean update = technologyService.updateById(technology);
        if (!update) {
            return R.error().message("更新失败");
        }
        return R.ok().data("success", true);
    }

    /**
     * 条件查询
     * @param technology 条件查询对象
     * @param pageNo 页码
     * @param limit 每页限制条数
     * @return 返回当页的记录和总数
     */
    @PostMapping("/listTechnology/{pageNo}/{limit}")
    public R listTechnology(@RequestBody(required = false) JobOpportunityTechnology technology,
                            @PathVariable Long pageNo,
                            @PathVariable Long limit) {
        QueryWrapper<JobOpportunityTechnology> queryWrapper =
                technologyService.queryTechnology(technology);
        Page<JobOpportunityTechnology> page = new Page<>(pageNo,limit);
        IPage<JobOpportunityTechnology> technologyPage = technologyService.page(page, queryWrapper);
        List<JobOpportunityTechnology> records = technologyPage.getRecords();
        long total = technologyPage.getTotal();
        return R.ok().data("records",records).data("total",total);
    }
}

